<?php
# 文件名称:user.php
# MetInfo在线反馈系统 
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
defined( '_SYS' ) or die( 'No direct script access allowed' );
class C_tableUser extends C_table
{
	/**
	 * 用户编号
	 *
	 * @var int
	 */
	var $id				= null;
	
	/**
	 * 用户类型
	 *
	 * @var string
	 */
	var $gid			= null;
	
	/**
	 * 登录用户名
	 *
	 * @var string
	 */
	var $admin_id		= null;
	
	/**
	 * 密码
	 *
	 * @var string
	 */
	var $admin_pass		= null;
	var $admin_name		= null;	
	var $admin_sex		= null;	
	
	var $admin_tel		= null;
	
	var $admin_mobile   = null;
	var $admin_email	= null;
	var $admin_qq		= null;
	
	var $admin_msn		= null;
	
	var $admin_taobao	= null;
	
	var $admin_introduction		= null;
	var $admin_login	= 0;
	
	var $admin_modify_ip		= null;
	
	var $admin_modify_date		= null;
	
	var $admin_register_date	= null;
	
	var $admin_approval_date	= null;
	
	var $block					= null;
	var $groups;
	
	var $params;
	
	function C_tableUser( &$db )
	{
		parent::__construct( $db->_table_prefix."admin_table", 'id', $db );
		$this->id        = 0;
	}
	function bind($array, $ignore = '')
	{
		$return = parent::bind($array, $ignore);
		// Load the real group data based on the bound ids.
		if ($return && !empty($this->groups))
		{
			// Set the group ids.
			C_loader::helper('array');
			toInteger($this->groups);
			$this->groups = array_fill_keys(array_values($this->groups), null);
			// Get the titles for the user groups.
			$this->_db->setQuery(
				'SELECT `id`, `title`' .
				' FROM `'.$this->_db->_table_prefix.'usergroups`' .
				' WHERE `id` = '.implode(' OR `id` = ', array_keys($this->groups))
			);
			$results = $this->_db->loadObjectList();
			// Check for a database error.
			if ($this->_db->getErrorNum()) {
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
			// Set the titles for the user groups.
			for ($i = 0, $n = count($results); $i < $n; $i++) {
				$this->groups[$results[$i]->id] = $results[$i]->title;
			}
		}
		return $return;
	}
	
	/**
	 * 过滤验证
	 *
	 * @return boolean
	 */
	function check()
	{
		if (trim( $this->admin_id ) == '') {
			$this->setError( 'Please enter a user name.' );
			return false;
		}
		if (eregi( "[<>\"'%;()&]", $this->admin_id) || strlen(utf8_decode($this->admin_id )) < 2) {
			$this->setError(  'Username Error' );
			return false;
		}
		if ((trim($this->admin_email) == "") ) {
			$this->setError( 'WARNREG_MAIL' );
			return false;
		}
		// check for existing username
		$query = 'SELECT id'
		. ' FROM  '.$this->_tb_name
		. ' WHERE admin_id = ' . $this->_db->Quote($this->admin_id)
		. ' AND id != '. (int) $this->id;
		;
		$this->_db->setQuery( $query );
		$xid = intval( $this->_db->loadResult() );
		if ($xid && $xid != intval( $this->id )) {
			$this->setError('WARNREG_INUSE');
			return false;
		}

		// check for existing email
		$query = 'SELECT id'
			. ' FROM  '.$this->_tb_name
			. ' WHERE admin_email = '. $this->_db->Quote($this->admin_email)
			. ' AND id != '. (int) $this->id
			;
		$this->_db->setQuery( $query );
		$xid = intval( $this->_db->loadResult() );
		if ($xid && $xid != intval( $this->id )) {
			$this->setError( 'WARNREG_EMAIL_INUSE' );
			return false;
		}
		return true;
	}
	function store( $updateNulls=false )
	{
		$k = $this->_tb_key;
		$key =  $this->$k;
		$SYS = &get_instance();
		// Store groups locally so as to not update directly.
		$groups = $this->groups;
		unset($this->groups);
		$user = $SYS->session->get('admin_name');
		//只有管理员可以操作权限
		if(in_array("1",explode(",",$user->gid)))
		{
			$this->gid = (count($groups) ? implode(',', array_keys($groups)) : $this->gid);
		}else $groups = array();
		
		if ($key)
		{
			// existing record
			$return = $this->_db->updateObject( $this->_tb_name, $this, $this->_tb_key, $updateNulls );
		}
		else
		{
			// new record
			$return = $this->_db->insertObject( $this->_tb_name, $this, $this->_tb_key );			
		}
			
		if( !$return )
		{
			$this->setError( strtolower(get_class( $this ))."::store failed<br />" . $this->_db->getErrorMsg() );
			return false;
		}
		// Reset groups to the local object.
		$this->groups = $groups;
		unset($groups);
		// Store the group data if the user data was saved.
		if ($return && is_array($this->groups) && count($this->groups))
		{
			// Delete the old user group maps.
			$this->_db->setQuery(
				'DELETE FROM `'.$this->_db->_table_prefix.'user_usergroup_map`' .
				' WHERE `user_id` = '.(int) $this->id
			);
			$this->_db->query();
			
			// Check for a database error.
			if ($this->_db->getErrorNum()) {
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
			// Set the new user group maps.
			$this->_db->setQuery(
				'INSERT INTO `'.$this->_db->_table_prefix.'user_usergroup_map` (`user_id`, `group_id`)' .
				' VALUES ('.$this->id.', '.implode('), ('.$this->id.', ', array_keys($this->groups)).')'
			);
			$this->_db->query();
			// Check for a database error.
			if ($this->_db->getErrorNum()) {
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
		}
		return true;
	}
	function delete( $oid=null )
	{
		$k = $this->_tb_key;
		if ($oid) {
			$this->$k = intval( $oid );
		}
		$query = 'DELETE FROM '. $this->_tb_name
		. ' WHERE '. $this->_tb_key .' = '. (int) $this->$k
		;
		$this->_db->setQuery( $query );
		if (!$this->_db->query())
		{
			$this->setError( $this->_db->getErrorMsg() );
			return false;
		}
		
		// Delete the user group maps.
		$this->_db->setQuery(
			'DELETE FROM `'.$this->_db->_table_prefix.'user_usergroup_map`' .
			' WHERE `user_id` = '.(int) $this->$k
		);
		$this->_db->query();
		// Check for a database error.
		if ($this->_db->getErrorNum()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
		return true;
	}
	/**
	 * 最后访问时间
	 *
	 * @param int The timestamp, defaults to 'now'
	 * @return boolean False if an error occurs
	 */
	function setLastVisit( $timeStamp=null, $id=null )
	{
		// check for User ID
		if (is_null( $id )) {
			if (isset( $this )) {
				$id = $this->id;
			} else {
				// do not translate
				
			}
		}
		$SYS =& get_instance();
		$SYS->load->helper('date');		
		$date = now();
		// updates user lastvistdate field with date and time
		$query = 'UPDATE '. $this->_tb_name
		. ' SET admin_modify_date = '.$this->_db->Quote($date->toMySQL())
		. ' WHERE id = '. (int) $id
		;
		$this->_db->setQuery( $query );
		if (!$this->_db->query()) {
			$this->setError( $this->_db->getErrorMsg() );
			return false;
		}
		return true;
	}
}
# 本程序是一个开源系统,使用时请你仔细阅读使用协议,商业用途请自觉购买商业授权.
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
?>